package www.nomina.controller.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import www.nomina.controller.beans.usuarioBean;
import www.nomina.model.dao.departamentosDao;
import www.nomina.model.dao.puestoDao;
import www.nomina.model.dao.turnoDao;
import www.nomina.model.dao.usuariosDao;

/**
 * Servlet implementation class homeSVR
 */
@WebServlet(name = "INTRANET", urlPatterns = { "/seguridadSVR" })
public class intranetSVR extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private String mapping = "/VIEW/COMMONS/index.jsp";

	usuariosDao usrDao = new usuariosDao();

	public intranetSVR() {
		super();
		System.out.println("HOME SVR EN EJECUCION");
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.home----------> doGet");
		mapping = "/VIEW/COMMONS/index.jsp";
		request.getRequestDispatcher(mapping).forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession sesion = request.getSession();
		
		puestoDao puestoDao = new puestoDao();
		departamentosDao deptoDao = new departamentosDao();
		turnoDao turnoDao = new turnoDao();
		
		System.out.println("SVR.home----------> doPost");

		int dispatch = Integer.parseInt(request.getParameter("dispatch"));
		System.out.println("SVR.usuarios.doPost---> Cargando el disparador");

		try {
			switch (dispatch) {
			case 0:// Login
				usuarioBean sesionBean = new usuarioBean();
				sesionBean.setRfc_usuario(request.getParameter("rfc"));
				String passTecleado = request.getParameter("secreta");

				if(usrDao.login(sesionBean)){
					// si existe validar secretas
					if (sesionBean.validarPassword(passTecleado)) {
						// abriendo sesion
						sesion.setAttribute("sesion", sesionBean);
						if(sesionBean.getStatus() ==1){
							mapping = "VIEW/SECURITY/intranet.jsp";
						}else{
							usrDao.activarCuenta(sesionBean.getId_usuario());
							mapping = "VIEW/SECURITY/activacion.jsp";
						}						
					} else {
						throw new Exception("password Incorrecto");
					}
				}else{
					throw new Exception("No existe usuario");
				}				
				
				break;
			case 1:// INDEX
				mapping = "VIEW/SECURITY/intranet.jsp";
				break;
			case 2:// NONIMAS
				mapping = "VIEW/SECURITY/NOMINAS/catalogo.jsp";
				break;
			case 3:// CATALOGO EMPLEADO
				mapping = "VIEW/SECURITY/EMPRESA/cat_empleados.jsp";
				break;
			case 4: // BUSQUEDA
				mapping = "VIEW/SECURITY/busqueda.jsp";
				break;
			case 5: // CONFIGURACION
				mapping = "VIEW/SECURITY/CUENTA/configuracion.jsp";
				break;
			case 6:// CATALOGO DEPTO				
				request.setAttribute("cat_depto",deptoDao.getCatalgo());
				mapping = "VIEW/SECURITY/EMPRESA/cat_departamentos.jsp";
				break;
			case 7:// CATALOGO NOMINA
				mapping = "VIEW/SECURITY/EMPRESA/cat_nominas.jsp";
				break;
			case 8:// about
				mapping = "VIEW/SECURITY/SISTEMA/about.jsp";
				break;
			case 9:// UPDATE SECRETA
				mapping = "VIEW/SECURITY/CUENTA/secreta.jsp";
				break;
			case 10:// BAJA DE CUENTA
				mapping = "VIEW/SECURITY/CUENTA/desabilitarCuenta.jsp";
				break;
			case 11:// EMPRESA
				mapping = "VIEW/SECURITY/EMPRESA/empresa.jsp";
				break;
			case 12:// DEPTO
				mapping = "VIEW/SECURITY/EMPRESA/depto.jsp";
				break;
			case 13:// PUESTO
				mapping = "VIEW/SECURITY/EMPRESA/puestos.jsp";
				break;
			case 14:// TURNO
				mapping = "VIEW/SECURITY/EMPRESA/turnos.jsp";
				break;
			case 15:// EMPLEADOS
				request.setAttribute("cat_puestos", puestoDao.getCatalgo());
				request.setAttribute("cat_turnos", turnoDao.getCatalgo());
				request.setAttribute("cat_depto",deptoDao.getCatalgo());
				mapping = "VIEW/SECURITY/EMPRESA/empleados.jsp";
				break;
			case 16:// CATALOGO PUESTOS				
				request.setAttribute("cat_puestos", puestoDao.getCatalgo());
				mapping = "VIEW/SECURITY/EMPRESA/cat_puestos.jsp";
				break;
			case 17:// CATALOGO TURNOS				
				request.setAttribute("cat_turnos", turnoDao.getCatalgo());
				mapping = "VIEW/SECURITY/EMPRESA/cat_turnos.jsp";
				break;
			}
			

		} catch (Exception error) {
			System.out.println("ChekSesion.doPost---->" + error.getMessage());
			request.setAttribute("mensaje", error.getMessage());
			mapping = "/VIEW/WARNINGS/errorForms.jsp";
		} finally {
			request.getRequestDispatcher(mapping).forward(request, response);
		}
	}

}
